using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraBars;
using E4RPDevExpress10;
using E4RPTools;
using Restaurant.Controls;

namespace Restaurant
{
    public partial class MainRibbonForm : DevExpress.XtraBars.Ribbon.RibbonForm
    {

        private Controller _controller;

        #region Properties

        public Controller Controller
        {
            set
            {
                _controller = value;
                _controller.OwnerWindow = this;
                dinnerRoomUserControl.Controller = _controller;
                requestUserControl.Controller = _controller;
                requestsUserControl.Controller = _controller;
                productManagementUserControl.Controller = _controller;
                tablesUserControl.Controller = _controller;                
                _controller.RequestShow += delegate { xtraTabControl.SelectedTabPage = requestXtraTabPage; };
            }
        }

        #endregion

        public MainRibbonForm()
        {
            InitializeComponent();
            xtraTabControl.SelectedPageChanged += delegate { SetControlsStatus(); };
            // Requests Group
            newRequestBarButtonItem.ItemClick += NewRequestBarButtonItemItemClick;
            saveRequestBarButtonItem.ItemClick += SaveRequestBarButtonItemItemClick;
            billRequestBarButtonItem.ItemClick += billRequestBarButtonItem_ItemClick;
            // PHC
            synchronizeBarButtonItem.ItemClick += SynchronizeBarButtonItemItemClick;
            // Tables Group
            newTableBarButtonItem.ItemClick += NewTableBarButtonItemItemClick;
            // Products Group
            addLevelBarButtonItem.ItemClick += AddLevelBarButtonItemItemClick;
            deleteBarButtonItem.ItemClick += DeleteBarButtonItemItemClick;
            // Layouts Group
            // Help Group
            aboutBarButtonItem.ItemClick += AboutBarButtonItemItemClick;
            exitBarButtonItem.ItemClick += ExitBarButtonItemItemClick;
            Closing += MainRibbonFormClosing;
            SizeChanged += MainRibbonFormSizeChanged;
            SetControlsStatus();
        }
        
        private void SetControlsStatus()
        {
            deleteBarButtonItem.Enabled = xtraTabControl.SelectedTabPage == productsXtraTabPage;
            addLevelBarButtonItem.Enabled = xtraTabControl.SelectedTabPage == productsXtraTabPage;
        }

        
        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);
            dinnerRoomUserControl.RestoreTables();
            _controller.NewRequest();
            xtraTabControl.SelectedTabPage = dinnerRoomXtraTabPage;
        }


        void NewRequestBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            if (!_controller.ExistsOpenedRequest(this, "Deseja mesmo efectuar um novo pedido?"))
                return;
            xtraTabControl.SelectedTabPage = requestXtraTabPage;
            _controller.NewRequest();
            SetControlsStatus();
        }

        void SaveRequestBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            requestUserControl.CloseEditors();
            requestUserControl.ClearControlErrors();
            Result result = _controller.IsRequestValid();
            if (result.Failed || _controller.Request.HasErrors)
            {
                requestUserControl.SetControlErrors(result);
                if (_controller.Request.Pedido[0].RowError != "")
                    MessageBox.Show(this, _controller.Request.Pedido[0].RowError, "Pedido", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if(_controller.SaveRequest(this))
                xtraTabControl.SelectedTabPage = dinnerRoomXtraTabPage;
        }

        void billRequestBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            throw new System.NotImplementedException();
        }


        void SynchronizeBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            _controller.SynchronizeProducts();
        }

        void NewTableBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            xtraTabControl.SelectedTabPage = tablesXtraTabPage;
            var control = xtraTabControl.SelectedTabPage.Controls[0];
            ((ITableList)control).AddTable();
        }

        void DeleteBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var control = xtraTabControl.SelectedTabPage.Controls[0];
            ((IProductManagement)control).Remove();
        }

        void AddLevelBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var control = xtraTabControl.SelectedTabPage.Controls[0];
            ((IProductManagement)control).AddGroup();
        }

        static void AboutBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var product = new AboutLabelControl("Restaurante", new Font("Jokerman", 24F, FontStyle.Regular, GraphicsUnit.Point, 0), Color.ForestGreen, new Point(127, 38));
            var form = new AboutXtraForm { Version = "1.0.0.0", Product = product };
            form.ShowDialog();
        }

        void ExitBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            _controller.CloseConnections();
            Close();
        }

        void MainRibbonFormClosing(object sender, CancelEventArgs e)
        {
            _controller.CloseConnections();
        }

        void MainRibbonFormSizeChanged(object sender, System.EventArgs e)
        {
            dinnerRoomUserControl.RestoreTables();
        }

    }
}